//
//  HELPayMethondConfirmVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/6.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELPayMethondConfirmVC.h"
#import "HELPayResultVC.h"
#import "BFPaperButton.h"
#import "NHCourseDetailVC.h"
#import "HELOrderListVC.h"
//#import <AlipaySDK/AlipaySDK.h>

#import "BTBaseWebVC.h"
@interface HELPayMethondConfirmVC ()
@property (weak, nonatomic) IBOutlet UILabel *priceL;
@property (weak, nonatomic) IBOutlet BFPaperButton *payBtn;





@property (nonatomic,  strong) MBProgressHUD *hud;

//支付字符串
@property (nonatomic,  strong) NSString *payInfo;
@end

@implementation HELPayMethondConfirmVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title  =@"订单支付";
    self.priceL.text = [NSString stringWithFormat:@"¥%.2f",self.price];
    [self.payBtn setBackgroundColor:[UIColor lightGrayColor]];
    [self getPayString];
  
    
    
    NoficationCenter_Obeserve(payResult:, Alipay_Result_noti);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_back"] style:UIBarButtonItemStyleDone target:self action:@selector(backToVC)];
    
}




- (IBAction)protolHtml:(id)sender {
    
    BTBaseWebVC *webVC = [[BTBaseWebVC alloc] init];
    NSString *url ;
    url = @"http://edu.sh-na.org.cn/nurseLearning-mobile/views/tradeNote.html";
    webVC.theUrl = [NSURL URLWithString:url];
            webVC.title = @"护慧用户付费协议";
    [self.navigationController pushViewController:webVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)payAction:(id)sender {
    
    //调出支付宝
     cwqHud(self.view);
     self.hud = hud;
  
    // NOTE: 调用支付结果开始支付
//    [[AlipaySDK defaultService] payOrder:self.payInfo fromScheme:@"com.shengshi.NursingAssociation" callback:^(NSDictionary *resultDic) {
//      
//    }];
    
   
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//     
//        
//    });
    
    
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)payResult:(NSNotification *)not{
    [self.hud hide:YES];
    
    weakSelf_define;
    NSDictionary *dic = not.userInfo;
    NSLog(@"reslut = %@",not.userInfo);
    HELPayResultVC *vc =[HELPayResultVC new];
    //支付成功//
    if (dic[@"resultStatus"] && [dic[@"resultStatus"] integerValue] == 9000) {
        [vc setresult:YES isFromCourse:[self isfromeCourse]];
        vc.clickBlock  =^(NSInteger index){
            if (index == 0) {
                [weakSelf backToVC];
            }
            
        };
        [self addChildViewController:vc];
        vc.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight- 64);
        [self.view addSubview:vc.view];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self backToVC];
        });
        

    }else{
        [vc setresult:NO isFromCourse:[self isfromeCourse]];
        vc.clickBlock  =^(NSInteger index){
            if (index == 0) {
                [weakSelf payAction:self.payBtn];
            }
            
        };
        vc.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight- 64);
        [self addChildViewController:vc];
        [self.view addSubview:vc.view];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           [UIView animateWithDuration:.35 animations:^{
                vc.view.alpha = 0;
           } completion:^(BOOL finished) {
               [vc.view removeFromSuperview];
               [vc removeFromParentViewController];
           }];
        });
    
    }
    
}

- (BOOL)isfromeCourse{
    
    __block  BOOL is = NO;
    [self.navigationController.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        if ([obj isKindOfClass:[NHCourseDetailVC class]]) {
            is = YES;
            return ;
        }
       
        
    }];
    
    return is;

}

- (void)backToVC{

    [self.navigationController.viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"%@, ----%@",obj.class , @(idx));
        if ([obj isKindOfClass:[NHCourseDetailVC class]]) {
            [self.navigationController popToViewController:obj animated:YES];
            NoficationCenter_post(RefreshCourseDetail);
            NoficationCenter_post(RefreshCourseContent);
            return ;
        }
        if ([obj isKindOfClass:[HELOrderListVC class]]) {
            NoficationCenter_post(RefreshOrderlist);
            NoficationCenter_post(RefreshCourseContent);
            [self.navigationController popToViewController:obj animated:YES];
        }
        
    }];

}

- (IBAction)isAgreeAction:(UIButton *)sender {
    
    sender.selected = ! sender.selected;
   
    self.payBtn.enabled = sender.selected;
    if (self.payBtn.enabled ==NO) {
         [self.payBtn setBackgroundColor:[UIColor lightGrayColor]];
    }else{
         [self.payBtn setBackgroundColor:[UIColor orangeColor]];
    }
   
   
}

//获取支付字符串
- (void)getPayString{
    
    [self wsj_showLodingGif];
    weakSelf_define;
    [[HELNetworkManager sharedMg] GET:[NSString stringWithFormat:@"%@/ios/%@",helGetOrderString,self.orderId] parameters:nil success:^(id responseObject) {
        [weakSelf wsj_hideLodingGif];
        
    
        if ([responseObject[@"data"][@"status"] isEqualToString:@"pay"]) {
           
         
           
            weakSelf.payInfo = responseObject[@"data"][@"payInfo"];
        
            
            //不需要支付
        }else if ([responseObject[@"data"][@"status"] isEqualToString:@"no_pay"]){
          
            cwqHudSuccess(@"参加课程成功~");
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf backToVC];
            });
        
            
        }else if ([responseObject[@"data"][@"status"] isEqualToString:@"no_pay"]){
            
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf backToVC];
        [weakSelf wsj_hideLodingGif];
       
    }];
    
}
@end
